package com.hzh.reverse.server.handler;

import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelHandler.Sharable;

@Sharable
public class PublicConnectionHandler extends ChannelInboundHandlerAdapter {

    private Channel innerClientChannel;

    public final static ConcurrentHashMap<String,Channel> channelMap=new ConcurrentHashMap<>();

    public final static Queue<Channel> channelQueue=new LinkedBlockingDeque<>();

    private String currentChannelKey;

    public PublicConnectionHandler(Channel innerClientChannel) {
        this.innerClientChannel = innerClientChannel;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("======公网请求=======》"+ctx.channel().remoteAddress());


        // channelQueue.add(ctx.channel());
        ctx.read();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        this.currentChannelKey=innerClientChannel.remoteAddress().toString();
        channelMap.put(currentChannelKey, ctx.channel());
        innerClientChannel.writeAndFlush(msg).addListener(new ChannelFutureListener(){
			@Override
			public void operationComplete(ChannelFuture arg0) throws Exception {
				System.out.println("完成写入内网客户端");	
			}
        });
    }
    
    
}
